package me.sealer.ssm.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import me.sealer.ssm.model.Dwz;
import me.sealer.ssm.service.DwzService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;

/**
 * 短网址服务
 *
 * @author sealer
 * @date 2017/06/19
 */
@Slf4j
@Controller
@Api(tags = "短网址跳转服务")
public class DwzController {
    private final DwzService dwzService;

    public DwzController(DwzService dwzService) {
        this.dwzService = dwzService;
    }

    @GetMapping(value = "/dwz/{shortUrl}")
    @ApiOperation(value = "通过短网址重定向到长网址")
    public void redirect(HttpServletResponse response,
                         @PathVariable(value = "shortUrl") @ApiParam(value = "本系统内短网址对应的地址码", required = true) String shortUrl) {

        Dwz dwz = dwzService.queryByShortUrl(shortUrl);
        response.setHeader("Location", dwz.getLongUrl());
        response.setStatus(302);
    }
}
